package com.ren.scale.controller;


import com.ren.scale.entity.ReclScale;
import com.ren.scale.entity.vo.ReclScaleQueryVo;
import com.ren.scale.entity.vo.ReclScaleVo;
import com.ren.scale.service.ReclScaleService;
import com.ren.utils.page.PageItem;
import com.ren.utils.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author RZY
 * @since 2022-07-13
 */

@Api(tags = "量表接口")
@RestController
@RequestMapping("/recl-scale")
public class ReclScaleController {

    @Autowired
    ReclScaleService scaleService;

    @ApiOperation(value = "查询指定id的量表数据")
    @GetMapping("/{id}")
    public R getScaleById(@PathVariable Long id) {
        ReclScaleVo scale = scaleService.getScaleVoById(id);
        return R.ok()
                .data("scale", scale);
    }

    @ApiOperation(value = "查询量表数据分页")
    @PostMapping("/{current}/{limit}")
    public R getScaleOfPage(@RequestBody(required = false) ReclScaleQueryVo reclScaleQueryVo,
                         @PathVariable long current,
                         @PathVariable long limit) {
        PageItem<ReclScaleVo> scalePageItem = scaleService.getScaleOfPage(reclScaleQueryVo, current, limit);
        return R.ok()
                .data("records", scalePageItem.getPageRecords())
                .data("total", scalePageItem.getTotal());
    }

    @ApiOperation(value ="修改指定id的量表数据")
    @PutMapping("")
    public R updateScaleById(@RequestBody ReclScale reclScale) {
        boolean res = scaleService.updateScaleById(reclScale);
        if(res) return R.ok()
                .message("量表修改成功！");
        else return R.error()
                .message("量表修改失败，请稍后再试！");
    }

    @ApiOperation(value = "删除指定id的量表数据")
    @DeleteMapping("/{id}")
    public R delScaleById(@PathVariable Long id) {
        boolean res = scaleService.delScaleById(id);
        if(res) return R.ok()
                .message("量表删除成功！");
        else return R.error()
                .message("量表删除失败，请稍后再试！");
    }

    @ApiOperation(value = "添加量表数据")
    @PostMapping("")
    public R addScaleById(@RequestBody ReclScale reclScale) {
        boolean res = scaleService.addScale(reclScale);
        if(res) return R.ok()
                .message("量表添加成功！");
        else return R.error()
                .message("量表添加失败，请稍后再试！");
    }
}

